import 'dart:ui';

import 'package:flutter/material.dart';

class CanvasDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final query = MediaQuery.of(context);
    return Scaffold(
//      appBar: AppBar(title: Text("CanvasDemo")),
      body: Center(
        child: CustomPaint(
          size: Size(query.size.width, query.size.height), //指定画布大小
          painter: MyPainter(),
        ),
      ),
    );
  }
}

class MyPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
//    canvasLine(canvas);
//    canvasPoint(canvas);
//    canvasPath(canvas);
  canvasRRect(canvas);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }

  void canvasLine(Canvas canvas) {
    final paint = Paint()
      ..isAntiAlias = true
      ..style = PaintingStyle.fill
      ..color = Colors.lightGreen
      ..strokeWidth = 10.0;

    canvas.drawLine(Offset(0, 0), Offset(60.0, 30.0), paint);
  }

  void canvasPoint(Canvas canvas) {
    final paint = Paint()
      ..isAntiAlias = true
      ..style = PaintingStyle.fill
      ..strokeWidth = 5.0
      ..color = Colors.blue;

    canvas.drawPoints(
        PointMode.points,
        [
          Offset(10, 10),
          Offset(30, 30),
          Offset(60, 60),
          Offset(90, 90),
          Offset(120, 60),
          Offset(150, 30),
        ],
        paint);
  }

  void canvasPath(Canvas canvas) {
    final paint = Paint()
      ..isAntiAlias = true
      ..style = PaintingStyle.stroke
      ..strokeWidth = 5.0
      ..color = Colors.blue;

//    final path = Path()
//    ..moveTo(0.0, 0.0)
//    ..lineTo(100, 200);
//
//    canvas.drawPath(path, paint);
//    Path path = new Path()..moveTo(100.0, 100.0);
//
//    path.lineTo(200.0, 200.0);
//
//    canvas.drawPath(path, paint);

    Path path = new Path()..moveTo(0.0, 0.0);

    path.lineTo(200.0, 200.0);
    path.lineTo(100.0, 300.0);
    path.lineTo(150.0, 350.0);
    path.lineTo(150.0, 500.0);

    canvas.drawPath(path, paint);
  }

  void canvasRRect(Canvas canvas) {
    final paint = Paint()
      ..isAntiAlias = true
      ..strokeCap = StrokeCap.round //画笔笔触类型
      ..style = PaintingStyle.fill
      ..strokeWidth = 5.0
      ..color = Colors.lightGreen;
//    Rect rect = Rect.fromCircle(center: Offset(150.0, 150.0), radius: 80.0);
    Rect rect = Rect.fromLTWH(50.0, 70.0, 180.0, 80.0);
    //根据上面的矩形,构建一个圆角矩形
    RRect rrect = RRect.fromRectAndRadius(rect, Radius.circular(10.0));
    canvas.drawRRect(rrect, paint);

    final path = Path();
    path.addRRect(rrect);

    path.moveTo(150.0 + 80, 80.0 + 10); // 100, 200
    path.lineTo(150.0 + 80 + 10, 80.0 + 10 + 10); // 110, 210
    path.lineTo(150.0 + 80, 80.0 + 10 + 10 + 10); // 100, 230

    canvas.drawPath(path, paint);

  }
}
